package com.orangelife.mobile.address.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.curry.android.adapter.FatherAdapter;
import com.curry.orangelife.mobile.R;


public class AddressListAdapter <T> extends FatherAdapter<T> {

	

	private Context context;
	private List<Map<String, Object>> listmap;

	public AddressListAdapter(Context context,
			List<Map<String, Object>> list) {

		this.context = context;
		this.listmap = list;
	}

	@Override
	public int getCount() {
		return listmap.size();
	}

	@Override
	public Object getItem(int position) {
		return listmap.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_address, null);
			holder.username=(TextView)convertView.findViewById(R.id.tv_username);
			holder.userphone=(TextView)convertView.findViewById(R.id.tv_userphone);
			holder.address=(TextView)convertView.findViewById(R.id.tv_address);
			holder.imageView=(ImageView)convertView.findViewById(R.id.iv_default_ship);
			holder.ll=(LinearLayout)convertView.findViewById(R.id.ll);
			holder.iv_Username=(ImageView)convertView.findViewById(R.id.iv_username);
			holder.iv_Phone=(ImageView)convertView.findViewById(R.id.iv_phone);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		Map<String, Object> map=listmap.get(position);
		if(map.get("isDefault").toString().equals("1")){
			holder.address.setText("[默认]"+map.get("addressJoin").toString());
			holder.ll.setBackgroundResource(R.color.address_brown);
			holder.address.setTextColor(context.getResources().getColor(R.color.text_color));
			holder.username.setTextColor(context.getResources().getColor(R.color.text_color));
			holder.userphone.setTextColor(context.getResources().getColor(R.color.text_color));
			holder.imageView.setBackgroundResource(R.drawable.my_default);
			holder.imageView.setVisibility(View.VISIBLE);
			holder.iv_Username.setBackgroundResource(R.drawable.default_ship_username);
			holder.iv_Phone.setBackgroundResource(R.drawable.default_ship_phone);
			
		}else{
			holder.address.setText(map.get("addressJoin").toString());
			holder.ll.setBackgroundResource(R.color.text_color);
			holder.iv_Username.setBackgroundResource(R.drawable.ship_username);
			holder.iv_Phone.setBackgroundResource(R.drawable.ship_phone);
			holder.imageView.setVisibility(View.INVISIBLE);
			holder.address.setTextColor(context.getResources().getColor(R.color.TextGray));
			holder.username.setTextColor(context.getResources().getColor(R.color.text_black));
			holder.userphone.setTextColor(context.getResources().getColor(R.color.text_black));
			
		}
			
		holder.username.setText(map.get("consignee").toString());
		holder.userphone.setText(map.get("phone").toString());
		
		// listview填数据
		return convertView;
	}

	class ViewHolder {
		// spinner里面的item
		private TextView username;
		private TextView userphone;
		private TextView address;
		private ImageView imageView;
		private ImageView iv_Username;
		private ImageView iv_Phone;
		private LinearLayout ll; 
	}
	
	@Override
	public BaseAdapter setList(List<T> list) {
		this.listmap=(List<Map<String, Object>>) list;
		return AddressListAdapter.this;
	}
}
